package com.runa.monitor.platinterface.netservice.equipment;

import com.runa.monitor.comm.dto.Archive;
import com.runa.monitor.comm.dto.Node;
import com.runa.monitor.comm.dto.newComm.ValveVO;
import com.runa.monitor.platinterface.backwork.function.TaskFunction;
import com.runa.monitor.platinterface.netservice.connect.commstatus.CommStatus;
import com.runa.protocol.util.UtilTime;
import com.runa.rccprotocol.dto.equdata.EquData;
import com.runa.rccprotocol.dto.feikong.FKNBRequestData;
import com.runa.rccprotocol.dto.param.ExtendParam;
import com.runa.rccprotocol.dto.param.NBIoTValveParam;
import com.runa.rccprotocol.dto.runa.RunaConcenAffirmDeny;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AutoUpwardDriver extends NetEqu<String> {

    private String model;

    private Map<String, NBIoTValveParam> valveParamMap = new HashMap<>();

    public AutoUpwardDriver(String serial, String model, TaskFunction reportFunction) {
        super(serial, null);
        this.model = model;
        reportFunction.setStatus(this);
        this.autoUpFunction = reportFunction;
    }

    public void upwardCallback(List equDataList) {
        //每次上报回调触发一次在线状态修改
        for (Object equData : equDataList) {
            if ("FeiKong_FMU".equals(model)) {
                if (equData instanceof ValveVO) {
                ValveVO valveVO = (ValveVO) equData;
                    NBIoTValveParam param = valveParamMap.get(valveVO.getValveSerial());
                    if (param == null) {
                        param = new NBIoTValveParam();
                        param.setIndex(valveVO.getIndex());
                        param.setLock(valveVO.getOpenLock());
                        param.setModel(valveVO.getFkModel());
                        param.setOpen(valveVO.getRemoteSet());
                        param.setTemp(valveVO.getTemperatureLockTarget());
                        valveParamMap.put(((ValveVO) equData).getValveSerial(), param);
                    }
                    param.setAddress(((ValveVO) equData).getValveSerial());
                    param.setDate(UtilTime.getCurrentTime("yyyyMMddHHmmss"));
                    param.setPeriod(valveVO.getReportPeriod());
                    param.setIp(valveVO.getIpv4());
                    param.setPort(valveVO.getIpPort());
                    param.setInterval(valveVO.getStandby7());
                    FKNBRequestData requestData = new FKNBRequestData(serial());
                    requestData.ctrlHouseValve(param);
                    sendRequest(requestData, null);
                }
            } else if ("HDRX_FMU".equals(model)) {

            }
        }
    }

    @Override
    public void readNodeEqu(List<Node> nodeSerial, TaskFunction function, EquData.EquDataType type) {

    }

    @Override
    public void setNodeEquArchive(List<Archive> archives, TaskFunction function) {

    }

    @Override
    public void clearNodeEquArchive(TaskFunction function) {

    }

    @Override
    public void valveOnOffControl(List<Node> valves, boolean ifLock, Integer opening, TaskFunction function) {
        for (Node valve : valves) {
            if (valve != null) {
                if ("FeiKong_FMU".equals(model)) {
                    NBIoTValveParam param = valveParamMap.get(valve.getSerial());
                    if (param == null) {
                        param = new NBIoTValveParam();
                        param.setIndex(valve.getIndex());
                        valveParamMap.put(valve.getSerial(), param);
                    }
                    param.setModel(3);
                    param.setLock(ifLock);
                    param.setOpen(opening);
                }
                function.setStatus(valve);
                function.doApply(CommStatus.OK, new RunaConcenAffirmDeny(true));
            }
        }
    }

    public void valveTemperatureLock(List<Node> valves, boolean ifLock, float temperature, TaskFunction function) {
        for (Node valve : valves) {
            if (valve != null) {
                if ("FeiKong_FMU".equals(model)) {
                    NBIoTValveParam param = valveParamMap.get(valve.getSerial());
                    if (param == null) {
                        param = new NBIoTValveParam();
                        param.setIndex(valve.getIndex());
                        valveParamMap.put(valve.getSerial(), param);
                    }
                    param.setModel(0);
                    param.setLock(ifLock);
                    param.setTemp(temperature);
                }
                function.setStatus(valve);
                function.doApply(CommStatus.OK, new RunaConcenAffirmDeny(true));
            }
        }
    }

    @Override
    public void readNodeRecord(List<Integer> indexes, TaskFunction function) {

    }

    @Override
    public void sentPublicUnitRecord(List<Node> valveSerial, Integer model, Map param, TaskFunction function) {

    }

    @Override
    public void sentPublicUnitModel(List<Node> valveSerial, boolean model, TaskFunction function) {

    }

    @Override
    public void sentExtendParam(ExtendParam param, TaskFunction function) {

    }

    @Override
    public void readPublicUnitRecord(Node valveSerial, Integer model, TaskFunction function) {

    }

    @Override
    public void readPublicUnitAndMeter(Node valveSerial, Node meterSerial, TaskFunction function) {

    }

    @Override
    public void heartbeatUpdate() {

    }

    @Override
    public boolean heartStop() {
        return false;
    }


}
